package com.beeasy.hzbpm.bean;

import com.github.llyb120.json.Arr;
import com.github.llyb120.json.Obj;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static com.beeasy.hzbpm.service.WorkService.work;
import static com.github.llyb120.json.Json.a;
import static com.github.llyb120.json.Json.o;

public class Util {

    public static Map<String, String> trueNameCache = new ConcurrentHashMap<>();
    public static Map<String, Set<String>> oidCache = new ConcurrentHashMap<>();
    public static Map<String, Boolean> suCache = new ConcurrentHashMap<>();

    public static String getUserCacheName(String uid) {
        String name = trueNameCache.get("true_name");
        if (name == null) {
            name = work.getUserCacheName(uid);
//            name = sqlManager.execute(new SQLReady("select true_name from t_user where id = ?", uid), Obj.class)
//                .stream()
//                .map(e -> e.s("true_name"))
//                .findFirst()
//                .orElse(null);
            trueNameCache.put(uid, name);
        }
        return name;
    }

    public static Obj getUserNamesMap(Collection<String> uids) {
        if (uids == null) {
            return o();
        }
        if (uids.size() == 0) {
            return o();
        }
        Obj ret = o();
        //拿到没有的
        Arr needSearch = a();
        for (String uid : uids) {
            String name = trueNameCache.get(uid);
            if(name == null){
                needSearch.add(uid);
            } else {
                ret.put(uid, name);
            }
        }
        if(!needSearch.isEmpty()){
            ret.putAll(work.getUserNamesMap(uids));
//            for (Obj obj : sqlManager.execute("select id, true_name from t_user where id in (#join(uids)#)", Obj.class, o("uids", needSearch))) {
//                ret.put(obj.s("id"), obj.s("true_name", ""));
//            }
            trueNameCache.putAll((Map)ret);
        }
        return ret;
    }

    public static boolean isSu(String uid) {
        Boolean su = suCache.get(uid);
        if (su == null) {
            su = work.isSu(uid);
//            su = sqlManager.execute(new SQLReady("select su from t_user where id = ?", uid), Obj.class)
//                .stream()
//                .map(e -> e.b("su"))
//                .findFirst()
//                .orElse(null);
//            su = su != null && su.equals(true);
            suCache.put(uid, su);
        }
        return su;
    }


//    public static Set<String> getUserOrg(String uid) {
//        Set<String> oid = oidCache.get(uid);
//        if (oid == null) {
//            oid = sqlManager.execute(new SQLReady("select oid from T_USER_ORG where uid = ?", uid), Obj.class)
//                    .stream()
//                    .map(e -> e.s("oid"))
//                    .collect(Collectors.toSet());
//            oidCache.put(uid, oid);
//        }
//        return oid;
//    }

}
